/*
 * ParamBoolean.java
 * 
 * Copyright 2004 Christoph Csallner and Yannis Smaragdakis.
 */
package edu.gatech.cc.cnc.params;

import de.fhg.first.fd.X;
import edu.gatech.cc.jcrasher.plans.expr.Expression;
import edu.gatech.cc.jcrasher.plans.expr.literals.BooleanLiteral;

/**
 * Represents a boolean variable / method parameter.
 * <p>
 * Uses an int solver, encodes false=0 and true=1.
 * 
 * @author csallner@gatech.edu (Christoph Csallner)
 */
public class ParamBoolean extends ParamIntegral<Boolean> {
	
	/**
	 * Constructor
	 */
	public ParamBoolean(String name) {		
		super(0, 1, name, booleanDomain);
	}

	
	/**
	 *  @returns X if constant=="true" or "false", null otherwise.
	 */
	@Override
	public X parse(String constant) {
		if (constant.equals("true")) {return solver.newX(1);}
		if (constant.equals("false")) {return solver.newX(0);}

		return parseError(constant);
	}
	
	
	/**
	 * Assumes that constraint system solution to which var belongs has been
	 * first prepared and then generated before.
	 */	
	@Override
	public Expression<Boolean> getPlanExpression() {
		int value = getValue();
		
		if (value==1)
			return new BooleanLiteral(true);	//TODO one instance enough!
		
		if (value==0)
			return new BooleanLiteral(false);
		
		throw new IllegalStateException();
		//TODO: Log consistency error.
	}
	
	@Override
	public String getTypeName() {
		return "boolean";
	}
	
	@Override
	public Class<Boolean> getType() {
		return Boolean.TYPE;
	}
}
